package com.briup.jd2511.jd2511_product_source.controller;

import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerAnimal;
import com.briup.jd2511.jd2511_product_source.result.Result;
import com.briup.jd2511.jd2511_product_source.service.ManagerAnimalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "动物管理模块")
@RestController
@RequestMapping("/animal")
public class AnimalController {

    @Autowired
    private ManagerAnimalService managerAnimalService;

    @GetMapping
    @Operation(summary = "分页+条件查询动物")
    @Parameters({
            @Parameter(name = "aHeathy",description = "动物健康状态",in = ParameterIn.QUERY),
            @Parameter(name = "aGender",description = "动物性别",in = ParameterIn.QUERY),
            @Parameter(name = "pageNum",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页大小",in = ParameterIn.QUERY),
    })
    public Result getAllByConditionByPage(@RequestParam(required = false) String aHeathy,
                                          @RequestParam(required = false) String aGender,
                                          Integer pageNum,
                                          Integer pageSize) {
        return Result.success(managerAnimalService.getAllByConditionByPage(aHeathy,aGender,pageNum,pageSize));
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增/修改动物信息")
    public Result saveOrUpdate(@RequestBody ManagerAnimal managerAnimal) {
        try {
            managerAnimalService.saveOrUpdate(managerAnimal);
            return Result.success();
        } catch (ServiceException e) {
            return Result.failure(e.getResultCode());
        }
    }
}
